<template>
  <dt-modal-edit ref="edit" title="案场呼入号配置" :width="900" @ok="onOk" :labelWidth="130"></dt-modal-edit>
</template>

<script>
  import editOpt from '../biz/caseCallInConfig.js'
  import apiMgr from '@/biz/caseReceiver/index'

  export default {
    data() {
      return {
        info: {},
        oldInfo: {
          housesName: '',
          buildingId: '',
          tospurLabel: '同策好房投放渠道',
          tcCity: '',
          tcInPhone: '',
          baiduLabel: '百度地图投放渠道',
          bdCity: '',
          bdInPhone: '',

          tospurVisible: false,
          baiduVisible: false
        },
        callInPage: {
          total: 0,
          page: 1,
          rows: 10,
          pages: ''
        },
        setReceiver: {}
      }
    },

    methods: {
      onOk(data) {
        if (data.tcInPhone && data.bdInPhone && data.tcInPhone == data.bdInPhone) {
          this.$Message.error('呼入号重复，请重新选择')
        } else {
          let params = {
            buildingId: data.buildingId,
            bdInPhone: data.bdInPhone,
            tcInPhone: data.tcInPhone,
            createId: this.$dt.userInfo().userId,
            updateId: this.$dt.userInfo().userId
          }
          apiMgr.buildingSaveOrUpdate(params).then((res) => {
            if (!res) {
              this.$Modal.confirm({
                title: '提示',
                content: `请继续配置承接人否则呼入电话无法打通`,
                okText: '前往配置',
                closable: true,
                onOk: () => {
                  this.$emit('setReceiver', this.setReceiver)
                  this.$refs.edit.hide()
                },
                onCancel: () => {
                  this.$emit('success')
                  this.$refs.edit.hide()
                  this.$Message.success('配置成功')
                }
              })
            } else {
              this.$emit('success')
              this.$refs.edit.hide()
              this.$Message.success('配置成功')
            }
          })
        }
      },
      getInit() {
        apiMgr.caseInCallConfigByBuildingId({ buildingId: this.info.buildingId }).then((res) => {
          this.info = { ...this.info, ...res }
          this.$refs.edit.show(editOpt.options(this), this.info)
        })
      },
      show(data) {
        this.setReceiver = data
        this.info = this.oldInfo
        this.info.buildingId = data.buildingId
        this.info.housesName =
          data.caseLocation + '-' + data.caseLocationTwo + '-' + data.department + '-' + data.buildingName
        this.info.tospurVisible = data.hfFlag == 1 ? true : false
        this.info.baiduVisible = data.bdFlag == 1 ? true : false
        this.getInit()
      }
    }
  }
</script>

<style lang="scss" scoped></style>
